<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\LearnVideoChapterStoreRequest;
use App\Models\LearnVideo;
use App\Models\LearnVideoChapter;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;
use Throwable;

class LearnVideoChapterController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|Response|object
     */
    public function index()
    {
        $list = LearnVideoChapter::filter(\request()->all())->orderByRaw('sort asc, id asc')->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param LearnVideoChapterStoreRequest $request
     * @return JsonResponse|object
     * @throws Throwable
     */
    public function store(LearnVideoChapterStoreRequest $request)
    {
        $params = $request->validated();

        try {
            DB::beginTransaction();
            $model = LearnVideoChapter::create($params);
            LearnVideo::where('id', $model->learn_video_id)->increment('chapter_count');
            DB::commit();
            return $this->restful($model);
        }catch (\Exception $e) {
            DB::rollBack();
        }

        return $this->restful(null, false, '创建失败');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param LearnVideoChapterStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(LearnVideoChapterStoreRequest $request, int $id)
    {
        $params = $request->validated();

        $result = LearnVideoChapter::where('id', $id)->update($params);

        return $this->restful($result);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     * @throws Throwable
     */
    public function destroy(int $id)
    {
        $result = 0;
        $model = LearnVideoChapter::where('id', $id)->first();
        if ($model) {
            try {
                DB::beginTransaction();
                LearnVideo::where('id', $model->learn_video_id)->decrement('chapter_count');
                LearnVideo::where('id', $model->learn_video_id)->decrement('section_count', $model->section_count);
                $model->delete();
                $result = 1;
                DB::commit();
            }catch (\Exception $e) {
                DB::rollBack();
            }
        }

        return $this->restful($result);
    }

    public function getOptions()
    {
        $list = LearnVideoChapter::select(['id as value', 'name as label'])->get()->toArray();

        return $this->restful($list);
    }
}
